﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Battleship
{
    public partial class Container : UserControl
    {
        private ContainerType containerType = ContainerType.DAN;
        private bool isOld = false, is40Feet = false, isDeparture = false;
        private bool isEmpty = false;

        private bool isHighLight = false;

        public void resetToEmpty()
        {
            this.isEmpty = true;
            this.txtName.Text = "";
            this.isHighLight = false;
            this.isOld = false;
            this.is40Feet = false;
            this.isDeparture = false;
        }

        public void setHighLightState()
        {
            this.isHighLight = true;
            this.Refresh();
        }

        public Container(Size containerSize, String containerIndex, ContainerType containerType = ContainerType.DAN, bool isOld = false, bool is40Feet = false, bool isDeparture = false)
        {
            InitializeComponent();

            // this.containerSize = containerSize;
            this.Size = containerSize;
            this.containerType = containerType;
            this.txtName.Location = new Point((this.Width - this.txtName.Width) / 2, (this.Height - this.txtName.Height) / 2);
            this.txtName.Text = containerIndex;
            this.isOld = isOld;
            this.is40Feet = is40Feet;
            this.isDeparture = isDeparture;
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        }

        public Container(ContainerModel container, int containerSize)
        {
            InitializeComponent();

            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Size = new Size(containerSize, containerSize);

            this.setContainerModel(container);
        }

        public void setContainerModel(ContainerModel container)
        {
            if (container == null)
            {
                this.resetToEmpty();
            }
            else
            {
                this.isOld = container.isOld;
                this.isDeparture = container.isDeparture;
                this.is40Feet = container.size == 2;
                this.containerType = container.containerType;

                this.txtName.Location = new Point((this.Width - this.txtName.Width) / 2, (this.Height - this.txtName.Height) / 2);
                this.txtName.Text = container.name;
                this.isHighLight = false;
                this.isEmpty = false;
            }

            this.Refresh();
        }

        public Container(int size)
        {
            InitializeComponent();
            this.Size = new Size(size, size);
            this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.isEmpty = true;
        }

        //public Size containerSize;

        Color corruptedColor = Color.Black;
        Color container40Color = Color.Black;
        Color departPortColor = Color.Indigo;
        Color arrivePortColor = Color.DarkGray;

        Color[] containerTypeColor = { Color.LightSalmon, Color.LightGreen, Color.LightBlue, Color.LightCyan };

        public Color getContainTypeColor()
        {
            Color color = Control.DefaultBackColor;

            switch (this.containerType)
            {
                case ContainerType.DAN:
                    color = containerTypeColor[0];
                    break;
                case ContainerType.EMP:
                    color = containerTypeColor[1];
                    break;
                case ContainerType.ORD:
                    color = containerTypeColor[2];
                    break;
                case ContainerType.REF:
                    color = containerTypeColor[3];
                    break;
            }

            return color;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics graphics = e.Graphics;
            if (isEmpty)
            {

                graphics.Clear(Control.DefaultBackColor);
                this.txtName.Visible = false;
                return;
            }

            this.txtName.Visible = true;

            int height = this.Height;
            int width = this.Width;

            if (this.isHighLight)
            {
                this.BackColor = Color.Pink;
            }
            else
            {
                this.BackColor = getContainTypeColor();
            }

            Point[] points = { new Point(0, 0), new Point(width / 4, 0), new Point(0, height / 4) };
            Brush brush = new SolidBrush(this.isOld ? corruptedColor : Color.Transparent);
            graphics.FillPolygon(brush, points);

            Point[] points2 = { new Point(0, height * 3 / 4), new Point(0, height), new Point(width / 4, height) };
            brush = new SolidBrush(this.isDeparture ? departPortColor : arrivePortColor);
            graphics.FillPolygon(brush, points2);

            Point[] points3 = { new Point(width * 9 / 10, 0), new Point(width, 0), new Point(width, height), new Point(width * 9 / 10, height) };
            brush = new SolidBrush(this.is40Feet ? container40Color : Color.Transparent);
            graphics.FillPolygon(brush, points3);
        }

        private void txtName_Click(object sender, EventArgs e)
        {
            this.OnClick(e);
        }

        private void Container_Load(object sender, EventArgs e)
        {

        }
    }
}
